package com.shrw.duke.bluetoothheartrate.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.shrw.duke.bluetoothheartrate.R;

/**
 * Created by rw-duke on 2017/11/6.
 */

public class PopupUtils {

    private final PopupWindow popupWindow;
    private final View mContentview;
    private boolean isShow;



    public PopupUtils( View contentview) {
//        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        mContentview = inflater.inflate(layoutId, null);
        this.mContentview = contentview;
        popupWindow = new PopupWindow(mContentview, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
//        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                isShow = false;
            }
        });

    }

    public static void setWindowAlpha(float alpha,Activity activity){
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha; //0.0-1.0
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        activity.getWindow().setAttributes(lp);
    }

    public void show(View view){
        isShow = true;
        popupWindow.showAtLocation(view, Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
    }

}
